package org.noworks.purdue.mapeditor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.LinkedList;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

class MapEditor extends JFrame {
	private static final long serialVersionUID = -919029780171658611L;
	// The preferred size of the demo
	private int PREFERRED_WIDTH = 680;
	private int PREFERRED_HEIGHT = 600;
	private JScrollPane scrollPane;
	private JMenuBar menu;
	private JMenu fMenu, eMenu, vMenu;
	private JMenuItem openMap, saveMap, saveMapAs, newMap, exit;
	private static String imgFile;
	private static double scale;

	private static LinkedList<Location> locs;
	private static LinkedList<Path> paths;

	public final static int VIEW = 0, EDIT = 1, INSERT_LOCATION = 2,
			REMOVE_LOCATION = 3, INSERT_PATH = 4, REMOVE_PATH = 5;

	public static int PROGRAM_STATE = EDIT;
	public static int EDITOR_STATE = INSERT_LOCATION;

	public static void main(String[] args) {
		MapEditor mapEditor = new MapEditor();
		mapEditor.setVisible(true);
	}

	public MapEditor() {
		setTitle("Map Editor");
		setSize(PREFERRED_WIDTH, PREFERRED_HEIGHT);
		setBackground(Color.gray);

		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		getContentPane().add(panel);

		locs = new LinkedList<Location>();
		paths = new LinkedList<Path>();

		menu = new JMenuBar();
		fMenu = new JMenu("File");
		eMenu = new JMenu("Edit");
		vMenu = new JMenu("View");
		openMap = new JMenuItem("Open");
		openMap.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fc = new JFileChooser();
				int returnVal = fc.showOpenDialog(MapEditor.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();
					XMLHandler.init(file, locs, paths, imgFile, scale);
				}
			}

		});
		saveMap = new JMenuItem("Save");
		saveMapAs = new JMenuItem("Save As");
		newMap = new JMenuItem("New");
		exit = new JMenuItem("Exit");

		fMenu.add(newMap);
		fMenu.add(openMap);
		fMenu.addSeparator();
		fMenu.add(saveMap);
		fMenu.add(saveMapAs);
		fMenu.addSeparator();
		fMenu.add(exit);

		menu.add(fMenu);
		menu.add(eMenu);
		menu.add(vMenu);
		panel.add(menu, BorderLayout.NORTH);

		XMLHandler.init("data/purdue.xml", locs, paths, imgFile, scale);

		Image image = new ImageIcon(imgFile).getImage();

		// draw(image.getGraphics());

		Icon icon = new ImageIcon(image);

		JLabel label = new JLabel(icon);

		scrollPane = new JScrollPane();
		scrollPane.getViewport().add(label);
		panel.add(scrollPane, BorderLayout.CENTER);
	}

	private void draw(Graphics g) {
		for (int idx = 0; idx < locs.size(); idx++)
			locs.get(idx).draw(g);
		for (int idx = 0; idx < paths.size(); idx++)
			paths.get(idx).draw(g);
	}
}
